package com.daJiao.exception;

import org.springframework.lang.Nullable;

   /**
    * <p>Title:      BusinessException. </p>
    * <p>Description 业务异常 </p>
    * <p>Copyright: Copyright (c) 2016</p>
    * <p>Company:    医百科技有限公司 </p>
    *
    * @author <a href="xinglinglove1029@163.com"/>杨文生</a>
    * @since 2020/12/30 14:15
    */
   public class BusinessException extends RuntimeException {

       private static final long serialVersionUID = -6319630941762529599L;
       /**
        * 异常码
        */
       private int code = 500;

       /**
        * 异常描述
        */
       private String message;

       private Object data;

       /**
        * 构造异常
        *
        * @param code    异常码
        * @param message 异常描述
        */
       public BusinessException(Integer code, String message, Throwable e) {
           super(message, e);
           this.code = code;
           this.message = message;
       }

       /**
        * 构造异常
        *
        * @param code    异常码
        * @param message 异常描述
        */
       public BusinessException(int code, String message) {
           super(message);
           this.code = code;
           this.message = message;
       }

       /**
        * 构造异常
        *
        * @param code    异常码
        * @param message 异常描述
        */
       public BusinessException(int code, String message, @Nullable Object data) {
           super(message);
           this.code = code;
           this.message = message;
           this.data = data;
       }

       public BusinessException(String message) {
           super(message);
           this.message = message;
       }

       public BusinessException(String message, Throwable e) {
           super(message, e);
           this.message = message;
       }

       public BusinessException(int code, String msgFormat, Object... args) {
           super(String.format(msgFormat, args));
           this.code = code;
           this.message = String.format(msgFormat, args);
       }

       public BusinessException(IExceptionEnum codeEnum) {
           super(codeEnum.getMsg());
           this.code = codeEnum.getCode();
           this.message = codeEnum.getMsg();
       }

       public BusinessException(IExceptionEnum codeEnum, Object... args) {
           super(String.format(codeEnum.getMsg(), args));
           this.code = codeEnum.getCode();
           this.message = String.format(codeEnum.getMsg(), args);
       }

       public BusinessException(IExceptionEnum codeEnum, @Nullable Object data) {
           super(codeEnum.getMsg());
           this.code = codeEnum.getCode();
           this.message = codeEnum.getMsg();
           this.data = data;
       }


       public int getCode() {
           return code;
       }

       public void setCode(int code) {
           this.code = code;
       }

       @Override
       public String getMessage() {
           return message;
       }

       public void setMessage(String message) {
           this.message = message;
       }

       public Object getData() {
           return data;
       }

       public void setData(Object data) {
           this.data = data;
       }

       @Override
       public String toString() {
           final StringBuffer sb = new StringBuffer("BusinessException{");
           sb.append("code=").append(code);
           sb.append(", message='").append(message).append('\'');
           sb.append(", data='").append(data).append('\'');
           sb.append('}');
           return sb.toString();
       }
   }
